package com.example.penggo;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

//follows is addded by user
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast; 
import android.view.KeyEvent;
import android.view.View.OnClickListener;


import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.PushService;
import com.parse.SignUpCallback;
import com.parse.ParseException;

public class MainActivity extends Activity {
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Parse.initialize(this, "xRWeO2RARaah3JsmOgFHF5YLj1dCuEthujG6w0h5", "fAdtJ4gwGObPLitsXGobU7ghpplsZh0NunoBIYrh"); 
		ParseAnalytics.trackAppOpened(getIntent());
/*		
		final ParseObject mTestbject = new ParseObject("SelfTest");	
		
		
		final ParseObject gameScore = new ParseObject("GameScore");
		gameScore.put("score", 1337);
		gameScore.put("playerName", "Sean Plott");
		gameScore.put("cheatMode", false);
		gameScore.saveInBackground();
*/
		final EditText username=(EditText)findViewById(R.id.username);
		final EditText password=(EditText)findViewById(R.id.password);		
		final EditText email=(EditText)findViewById(R.id.email);
		
		PushService.subscribe(this, "PengGoFans", PushNotifyActivity.class);
		PushService.setDefaultPushCallback(this, CloudTestActivity.class);
		ParseInstallation.getCurrentInstallation().saveInBackground();		
		
        //��ȡEditText�ı�  
		Button Button=(Button)findViewById(R.id.Regist);  
		Button.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
            	
            	if(username.getText().toString().isEmpty()){
            		Toast.makeText(MainActivity.this, "usename is empity!", Toast.LENGTH_SHORT).show();      
            		return;
            	}else if(password.getText().toString().isEmpty()){
            		Toast.makeText(MainActivity.this, "password is empity!", Toast.LENGTH_SHORT).show();          
            		return;
            	}else if(username.getText().toString().isEmpty()){
            		Toast.makeText(MainActivity.this, "email is empity!", Toast.LENGTH_SHORT).show();           
            		return;
            	}
            	
            	
	        	ParseUser user = new ParseUser();
	        	user.setUsername(username.getText().toString());
	        	user.setPassword(password.getText().toString());
	        	user.setEmail(email.getText().toString());
	        	
	        	user.signUpInBackground(new SignUpCallback() {
	        		  public void done(ParseException e) {
	        		    if (e == null) {
	                        Toast.makeText(MainActivity.this, "Regist Successful!", Toast.LENGTH_SHORT).show();  
	        		    } else {
	                        Toast.makeText(MainActivity.this, "Regist Fail!", Toast.LENGTH_SHORT).show();  
	        		    }
	        		  }
	        		});            		

            }  
        });  	
		
		
		Button cloudButton=(Button)findViewById(R.id.cloudtest);  
		cloudButton.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {

            	startActivity(new Intent(MainActivity.this,HttpAPITestActivity.class));
            }  
        });  		
	}

	public class MyCustomReceiver extends BroadcastReceiver {
		private static final String TAG = "MyCustomReceiver";
		 
		  @Override
		  public void onReceive(Context context, Intent intent) {
		    try {
		      String action = intent.getAction();
		      String channel = intent.getExtras().getString("com.parse.Channel");
		      JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
		 
		      Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
		      Iterator itr = json.keys();
		      while (itr.hasNext()) {
		        String key = (String) itr.next();
		        Log.d(TAG, "..." + key + " => " + json.getString(key));
		      }
		    } catch (JSONException e) {
		      Log.d(TAG, "JSONException: " + e.getMessage());
		    }
		  }
		}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
